package com.example.schedulecard.Databases;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import androidx.annotation.NonNull;

import com.example.schedulecard.Entity.Schedule;

import java.util.ArrayList;
import java.util.List;

public class ScheduleFestOption {
//    创建课表清单数据库
    public static ScheduleFestDatabase CreateScheduletable(Context context) {
        return new ScheduleFestDatabase(context);
    }

//    添加课程表
    public static int addSchedule(@NonNull SQLiteDatabase sqLiteDatabase, @NonNull Schedule schedule) {
        ContentValues values = new ContentValues();
        values.put("schedulename", schedule.getSchedulename());
        return (int) sqLiteDatabase.insert("schedulefest", null, values);
    }

//    删除课程表
    public static int deleteSchedule(@NonNull SQLiteDatabase sqLiteDatabase, @NonNull Schedule schedule) {
        String sql = "schedulename=?";
        return (int) sqLiteDatabase.delete("schedulefest", sql
                , new String[]{schedule.getSchedulename()});
    }

//    查询课程表
    public static List<Schedule> querySchedule(@NonNull SQLiteDatabase sqLiteDatabase) {
        List<Schedule> schedulesList = new ArrayList<>();
        Cursor cursor = sqLiteDatabase.rawQuery("select * from schedulefest", null);
//        遍历课程表信息
        while (cursor.moveToNext()) {
            schedulesList.add(new Schedule(
                    cursor.getString((int) cursor.getColumnIndex("schedulename"))
                    , cursor.getInt((int) cursor.getColumnIndex("worksum"))
                    )
            );
        }
        cursor.close();
        return schedulesList;
    }

//    更新单个课表信息
    public static int updataSchedule(SQLiteDatabase sqLiteDatabase, Schedule schedule){
        ContentValues values = new ContentValues();
//        存储新的课表信息
        values.put("worksum",schedule.getWorksum());
        String sql = "schedulename=?" ;
        return sqLiteDatabase.update("schedulefest",values,sql,new String[]{
              schedule.getSchedulename()
        });
    }
}
